//
// Created by 刘禹晨 on 16/7/21.
// Copyright (c) 2016 yuchenliu. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "MainViewController.h"
#import "AppDelegate.h"
#import "LeftMenuCell.h"
#import "MenuModel.h"
#import "MMDrawerController/UIViewController+MMDrawerController.h"
#import "SettingViewController.h"
#import "AboutController.h"

#define MaxWidth [UIScreen mainScreen].bounds.size.width
#define MaxHeight [UIScreen mainScreen].bounds.size.height

@interface LeftMenuViewController() <UITableViewDataSource, UITableViewDelegate>
{
    NSArray *dataSet;
    UITableView *tableView;
    MainViewController *mainCV;
    AppDelegate *appDelegate;
    NSDictionary *receiveDistionary;
    MenuModel *menuModel1;
    MenuModel *menuModel2;
    MenuModel *menuModel3;
    MenuModel *menuModel4;
    MenuModel *menuModel5;
    MenuModel *menuModel6;
    MenuModel *menuModel7;
    MenuModel *menuModel8;
    
    UINavigationBar *uinavigationBar;
}
@end

@implementation LeftMenuViewController

- (void)viewDidLoad {
    appDelegate = [[UIApplication sharedApplication] delegate];
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MaxWidth / 1.5, MaxHeight) style:UITableViewStyleGrouped];

//    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MaxWidth, 64.0)];
//    [tableView setTableHeaderView:header];
//    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MaxWidth, 48.0)];
//    [tableView setTableFooterView:footer];

    [tableView registerClass:[LeftMenuCell class] forCellReuseIdentifier:@"LeftMenuCell"];

    tableView.delegate = self;
    tableView.dataSource = self;
    [self initNavigationBar];

    [self initDataSet];
    dataSet = @[
            @[
                    @"蓝牙",
                    menuModel5,
                    menuModel1,
                    menuModel2
            ],
            @[
                    @"WiFi",
                    menuModel6,
                    menuModel7,
                    menuModel8
            ],
            @[
                    @"设置",
                    menuModel3,
                    menuModel4
            ]
    ];

    [self.view setBackgroundColor:[[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f]];
    [tableView setBackgroundColor:[[UIColor new] initWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f]];
    //[tableView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftbg"]]];
    [self.view addSubview:tableView];


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDataSet:) name:@"leftMenuDataSet" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateWifiState:) name:@"leftMenuDataSetWiFi" object:nil];
}

-(void)initDataSet
{
    menuModel1 = [[MenuModel alloc]init];
    menuModel1.menuTitle = @"蓝牙名称";
    menuModel1.hasSwitch = @"NO";
    menuModel2 = [[MenuModel alloc]init];
    menuModel2.menuTitle = @"信号强度";
    menuModel2.hasSwitch = @"NO";
    menuModel3 = [[MenuModel alloc]init];
    menuModel3.menuTitle = @"WiFi开关";
    menuModel3.hasSwitch = @"YES";
    menuModel4 = [[MenuModel alloc]init];
    menuModel4.menuTitle = @"关于";
    menuModel4.hasSwitch = @"NO";
    menuModel5 = [[MenuModel alloc]init];
    menuModel5.menuTitle = @"蓝牙状态";
    menuModel5.hasSwitch = @"NO";
    menuModel6 = [[MenuModel alloc]init];
    menuModel6.menuTitle = @"WiFi状态";
    menuModel6.menuDetail = @"Socket未连接";
    menuModel6.hasSwitch = @"NO";
    menuModel7 = [[MenuModel alloc]init];
    menuModel7.menuTitle = @"IP地址";
    menuModel7.hasSwitch = @"NO";
    menuModel8 = [[MenuModel alloc]init];
    menuModel8.menuTitle = @"WiFi设置";
    menuModel8.hasSwitch = @"NO";
}

-(void)initNavigationBar
{
    uinavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, MaxWidth / 1.5, 64)];
    [uinavigationBar setTranslucent:NO];
    uinavigationBar.barTintColor = [[UIColor new] initWithRed:255.0f/255.0f green:73.0f/255.0f blue:64.0f/255.0f alpha:1.0f];
    UINavigationItem *uiNavigationItem = [[UINavigationItem alloc]init];
    [uiNavigationItem setTitle:@"菜单"];
    NSMutableDictionary *barsytle = [NSMutableDictionary dictionary];
    [barsytle setObject:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0f] forKey:NSForegroundColorAttributeName];
    [uinavigationBar setTitleTextAttributes:barsytle];
    [uinavigationBar pushNavigationItem:uiNavigationItem animated:NO];
    [self.view addSubview:uinavigationBar];
}

-(void)updateDataSet:(NSNotification *)notify
{
    //NSLog(@"Notify:%@",notify);
    receiveDistionary = notify.userInfo;
    
    menuModel1.menuDetail = [receiveDistionary valueForKey:@"name"];
    menuModel2.menuDetail = [receiveDistionary valueForKey:@"RSSI"];
    menuModel5.menuDetail = [receiveDistionary valueForKey:@"state"];
    [tableView reloadData];
}

-(void)updateWifiState:(NSNotification *)notify
{
    NSLog(@"Notify:%@",notify);
    receiveDistionary = notify.userInfo;
    
    menuModel6.menuDetail = [receiveDistionary valueForKey:@"state"];
    menuModel7.menuDetail = [receiveDistionary valueForKey:@"IP"];

    [tableView reloadData];
}

#pragma mark - TableView Delegate

/*
 * 返回分组数目
 */
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [dataSet count];
}

/*
 * 返回每个分组的行数
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [dataSet[section] count] - 1;
}

/*
 * 返回分组标题间隔
 */
-(CGFloat)tableView :(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

-(CGFloat)tableView :(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

/*
 * 每一个分组下对应的tableview 高度
 */
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;

    static NSString *TableSampleIdentifier = @"test";

    LeftMenuCell *cell = [self->tableView dequeueReusableCellWithIdentifier:
            TableSampleIdentifier];
    if(!cell){
        cell = [[LeftMenuCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TableSampleIdentifier];
    }

    MenuModel *model = dataSet[section][row + 1];
    cell.menuModel = model;
    if((section == 1 && row == 2) || (section == 2 && row == 1)){
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }else{
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;

//    NSInteger section = indexPath.section;
//    NSInteger row = indexPath.row;
//    static NSString *CellIdentifier = @"Cell";
//
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    }
//    cell.textLabel.text = dataSet[section][row + 1];
//    [cell setBackgroundColor:[UIColor whiteColor]];
//    cell.textLabel.textColor = [UIColor grayColor];
//    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return dataSet[section][0];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@",indexPath);
    UITabBarController *cen = (UITabBarController*)self.mm_drawerController.centerViewController;
    NSInteger selectIndex = cen.selectedIndex;
    UINavigationController *cens = (UINavigationController *)[cen.childViewControllers objectAtIndex:selectIndex];
    if(indexPath.row == 2 && indexPath.section ==1)
    {
        SettingViewController *settingView = [[SettingViewController alloc]init];
        [settingView setHidesBottomBarWhenPushed:YES];
        [cens pushViewController:settingView animated:NO];
        [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
            
        }];
    }
    if(indexPath.row == 1 && indexPath.section == 2)
    {
        AboutController *aboutView = [[AboutController alloc]init];
        [aboutView setHidesBottomBarWhenPushed:YES];
        [cens pushViewController:aboutView animated:NO];
        [self.mm_drawerController closeDrawerAnimated:YES completion:^(BOOL finished) {
            
        }];
    }
    [self->tableView deselectRowAtIndexPath:indexPath animated:YES];

}

-(void)closeDrawerAnimated:(BOOL)animated viewController:(UIViewController *)panel completion:(void (^)(BOOL finished))completion
{
    NSLog(@"push");
}

/**
 * 设置白色状态栏
 */
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

@end